﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class XAPHUONGService : BaseService
    {

        public static bool Check_Insert_XAPHUONG(XAPHUONG xaphuong)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<XAPHUONG>().FirstOrDefault(c => c.TENXAPHUONG.ToUpper() == xaphuong.TENXAPHUONG.ToUpper()
                && c.MAQUANHUYEN == xaphuong.MAQUANHUYEN) == null;
            return result;
        }

        public static bool Check_Update_XAPHUONG(XAPHUONG xaphuong)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<XAPHUONG>().FirstOrDefault(c => c.TENXAPHUONG.ToUpper() == xaphuong.TENXAPHUONG.ToUpper()
                && c.MAQUANHUYEN == xaphuong.MAQUANHUYEN && c.MAXAPHUONG != xaphuong.MAXAPHUONG) == null;
            return result;
        }

        public static bool Check_Delete_XAPHUONG(string[] strings)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<HOGIADINH>().FirstOrDefault(c => strings.Contains(c.MAQUANHUYEN + "")) == null;
            return result;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_City_Country_District()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Xã Phường} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<QUOCGIA>()
                .Join(dataContext.GetTable<TINHTHANH>(), c => c.MAQUOCGIA, v => v.MAQUOCGIA, (c, v) => new { v.MATINHTHANH, TENTINHTHANH = v.TENTINHTHANH + " - " + c.TENQUOCGIA })
                .Join(dataContext.GetTable<QUANHUYEN>(), c => c.MATINHTHANH, v => v.MATINHTHANH, (c, v) => new { v.MAQUANHUYEN, TENQUANHUYEN = v.TENQUANHUYEN + " - " + c.TENTINHTHANH })
                .Join(dataContext.GetTable<XAPHUONG>(), c => c.MAQUANHUYEN, v => v.MAQUANHUYEN, (c, v) => new { v.MAXAPHUONG, TENXAPHUONG = v.TENXAPHUONG + " - " + c.TENQUANHUYEN }
                ).OrderBy(c => c.TENXAPHUONG), "MAXAPHUONG", "TENXAPHUONG"));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_City_Country_District_Data()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Xã Phường} ---", Value = "" });
            list.AddRange(dataContext.GetTable<QUOCGIA>().Where(c => c.ISDATA ?? false)
                .Join(dataContext.GetTable<TINHTHANH>().Where(c => c.ISDATA ?? false),
                      c => c.MAQUOCGIA, v => v.MAQUOCGIA,
                      (c, v) => new { v.MATINHTHANH, TENTINHTHANH = v.TENTINHTHANH + " - " + c.TENQUOCGIA })
                .Join(dataContext.GetTable<QUANHUYEN>().Where(c => c.ISDATA ?? false),
                      c => c.MATINHTHANH, v => v.MATINHTHANH,
                      (c, v) => new { v.MAQUANHUYEN, TENQUANHUYEN = v.TENQUANHUYEN + " - " + c.TENTINHTHANH })
                .Join(dataContext.GetTable<XAPHUONG>().Where(c => c.ISDATA ?? false),
                      c => c.MAQUANHUYEN, v => v.MAQUANHUYEN,
                      (c, v) => new { v.MAXAPHUONG, TENXAPHUONG = v.TENXAPHUONG + " - " + c.TENQUANHUYEN, v.ISDEFAULT }
                ).OrderBy(c => c.TENXAPHUONG).Select(
                    c =>
                    new SelectListItem() { Text = c.TENXAPHUONG, Value = c.MAXAPHUONG + "", Selected = c.ISDEFAULT ?? false }));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_City_Country_District(string id)
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Xã Phường} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<QUOCGIA>()
                .Join(dataContext.GetTable<TINHTHANH>(), c => c.MAQUOCGIA, v => v.MAQUOCGIA, (c, v) => new { v.MATINHTHANH })
                .Join(dataContext.GetTable<QUANHUYEN>(), c => c.MATINHTHANH, v => v.MATINHTHANH, (c, v) => new { v.MAQUANHUYEN })
                .Join(dataContext.GetTable<XAPHUONG>(), c => c.MAQUANHUYEN, v => v.MAQUANHUYEN, (c, v) => new { v.MAXAPHUONG, v.TENXAPHUONG }
                ).OrderBy(c => c.TENXAPHUONG), "MAXAPHUONG", "TENXAPHUONG"));
            return list;
        }
    }
}
